Bug fixes in stats routines