fix edge detection conditions; optimise slightly