fixed up main loop