fix bugs in implementation