improve logic for calculation