Convert some untyped variables to typed variables, replace custom Sum function with built-in function (in some locations).