improved eroded function