added comments, fixed a bug.