Code quality improvements and using min() in a good way