added comments, added additional arguments, some code cleanup