fixed some bugs; added comments