Reorganize API and write new tests