Fixed some bugs introduced in refactoring