added averaging code and truncating code