Cleaned up the original source code, added comments.