optimization and bugfix