Many bug fixes and performance improvements