Clean up avg code and make it closer to what blockexplorer gives