Cleaned up code and added comments. More similar commits to follow.