more refactoring for speed