Minor bugfixes and code cleanup