General logic for calculation, will refactor