Made more fault tolerant and gave more explicit error message.