added comments and bug fixes