Based on http://www.newosxbook.com/src.jl?tree=listings&file=18-1-JKextstat.c