Fixed error checking to handle errors more nicely