Simplify apply_median_filter function