Error handling added for main method