wrote magnet function