Series of bugfixes including #86

Closed #22 and #83