added comments, need to check a few redundant snipets of code