Fixed a bug in average function. --Y.Cao