added comments and organized some code