added smart resize function