Refactoring a lot of code

Doing more work to make it more cross-platform friendly.