added debug and updated error messages