moving function out and addign try catch