Bug fix

Same as previous with minor bugfixes.