fixed bug with error check