split out stuff from main loop