Refactored code to create the command string into a common function