More robust IO, better error checking