More bug fixes after refactoring