Re-implemented median filtering as a function.