Scientists finally discover how magnets work