Major refactoring and linux compatability