More efficient code for remove_empty.