Initial refactoring to seperate out os-dependent code from the logic.
Build tweaked to be slightly less os-dependent.