added comments, fixed bugs