Minor bugfixes and optimization