Cleaned up source code, added comments