performance improvements and code cleanup