improved contour detection