Small performance improvements and code cleanup