Moved code into main(), added documentation, changed usage.

* Moved loose code into main().
* Added target path as argument, with usage printed on error.
* Added docstrings to key functions.