added check before attempting to remove elements