Added eroding and dilating logic