Performance improvements and code cleanup