More refactoring. Everything seems to work.