A more working version of everything. Cleaned up the main loop.