added try/catch and loops for error catching