Cost function dependence on size - batch gradient descent
Your second change (calculating the average error) is the correct method. Imagine this: if there are billion examples in your training set, even if you make (very) small error on each, the sum total is going to add up to a large number.
So the cost function in case of least squared is mean squared error, not just sum.