Performance optimizations

Performance optimizations from issue 1154