fixed all the bugs introduced by refactoring