Improved code for CLI tool, new parameter --wait.