general readability and prettification changes, added clamp function