the code is still duplicated in many places, please centralize this calculations in only once place