Bugfixes, mainly related to variable scopes