refactored stats functions and add stats test