Refactored the handling of stop command