added proper error management