modify detect_end logic