err handling should be inside the else condition