Added double to sum, removed external loops