foldBy refactoring