Stats performance improvements

Includes the commits of PR #288