Refactored CopyToDevice code.

- Basic comments added.
- NOTE: Code still does not work. It fails on the file write.
Also the code has to be extended to work with copying directories and
their contents.