Refactoring. Fix bugs introduced by refactoring.