more refactoring, for correctness