Use foldLeft instead of var and for loop