added comments and abstracted some of the methods