further refactoring/optimizations