Refactored and added stats handler