Added code with performance improvements