Re-apply pull request #10, it got lost for some reason