Revert "Revert "refactoring""

This reverts commit fc94c4effdf416b5169a05b1d247f2c714e4b96f.
Refactor code and add OS recognition.