added comments, fixed code