Get the relevant stuff from issue #16.

From ac82b28.