minor performance tweaks and refactoring