moved some things out of main loop