improve style and fix avg calculation