Refactor Engine#handle_signal to avoid code duplication