Fix errors and change error trapping

More error trapping to follow