Thanks to imurray, who suggested a different algorithm for calculating variance