Rearranged core loop order to preserve cursor location