cleanup, small refactorings
simplify merging, move to own file, fix bug