Cleaned up code and Added comments

Cleaned up and re-arranged some elements of the code, removed a function
that wasn't doing anything, and added comments.