performance improvements and bugfix