added comments and some bug fixes