working version with fixes and better stats