added comments, debugged