Bug fix and performance improvements