Restructured code to add error to callbacks