Inserted some error checking