[api] split the big function into smaller ones

Most of them are still too large, but we need to see the test suite
passing on the way