Add error checking and fix variance calculation.