added some code for error detecting