Bugfixes by martin.
Merge martin's changes and nikolaj and stefan changes.