Refactor communication with API