added comments, clean up code