optimized database, created queries for stats