fix bugs in the last refactor