Added clamp function