Moved functions directly into main loop.