Cleaned up some code and added a few comments