Standardized clamp<>, fixed out-of-bounds bugs. Added asserts to catch
stuff like that earlier.