Refactored creation of API client and fixed crash