added clamp function