Fixes #281 and #286, thanks @appetito for debugging this.