Replaced repeated code, and added an exit command.