Performance improvement and bugfix