Add much better error checking