Fix some bugs that were introduced when refactoring code