working version using mean