Fix optimal width calculation and refactoring.