Bugfix

Lots of changes, detailed in each pull request