Refactor OS-specific code, implement OS X functionality; fixes #5