Refactored perform_commands.