bugfixes and optimization