ported directly from my bitbucket