fixed some monkey patching bugs and added comments