manual folding; more refactoring