Fixed for linux copy. Refactored a bit of the code.