Fixed bugs

There were some bugs introduced after my original refactoring, but they have been squashed.