Introduce a generic clamp function to clean up some of the various 'if-else' clamps throughout parts of the codebase.