A revised version of PR #330 that passes all specs.

This is basically PR #330 with passing specs.