Rebased #299

Rebase @toumorokoshi's work against the latest master to make it merge.

This closes #299