refactoring of stat.py