cleanups and command-line arguments

Small refactor that make it easier to read and also to integrate this code with other apps.
Feel free to ignore this PR if it's not relevant or needed.