Fixes #39 as best as possible. Might need further inspection.