added averages and some error handling