Replaces PR #900

This is just a rebase of #900. Thanks @shytikov!