better search for missing pixels, better gradient function