Cleaned up some code, added comments