some logic and Math optimisation