Pull request #445, rebased

I just rebased #445 on top of the latest master and resolved the conflicts.