Resubmitting the pull request with some more improvements