Fix an edge case and improve tests for API