moved code out of try catch block