Moved the remove_nils method to a private method