moved calculation of error to main