Better edge detection.

Please pull my changes, I think they are good.