I'm not sure if this is correct, but it fixes the issue #2, if it isn't correct, close the pull request.