cleaned up code

tried to make it more readable, and added comments.