Fix bugs in implementation