re-writen for osx