Refactoring, mostly for performance