Fixed several bugs in the refactoring.