Performance improvements and refactors