Added error handling for type conversion