Cleaned up code, added comments, renamed things