Lots of backend fixes

Decided not to rewrite in the end