Corrected error handeling to be done globally