Fixed the clamping function