First draft of stats code, probably not working yet.