Clamp function to remove redundant code