More refactoring

Refactored and added tests for history, challenge_random, detailed_challenge_stats and challenge_stats.