Bugfixes and code improvements