Fixes #9 and possibly #11 (more testing needed).