Pre-refactor code with various performance improvements