simplified code into one try-except to make it effective