extract logic for edge detection