Refactored sensor averaging code.