A lot of repeated code between `.empty` and `.clear`. Would be good to extract the common code into a helper function.