Refactoring, moved code around