Some refactoring and cleanup, also add --log-level