Cleaned up some code and fixed some final bugs.