Cleaned up main loop