bugfix + optimizations