More bug fixes from refactoring