refactored the withCommand function