calculating total sum in a nested data structure