Fix bug in calculate_stats