Refactor command line and code cleanup