Scrap first attempt at a higher-level api, clean up low-level code