Resolves #17 plus some misc fixes.