Use Math.min and Math.max method instead of manual calculations.