moved a whole bunch of functions around for better code clarity. it's 3am and i can't sleep, so i vaguely expect to have to revert this in the morning.