Fixed bugs after refactoring