added comments, cleaned up some code