Partial refactor of api_sapy