extracted clampborder function