refactored processCommand method