fixed some bugs in implementation