corrected edge detection so it detects the first edge instead of the last edge.