originally from bitbucket.org/kardianos/osex