Perhaps move these 2 statements under the if statement (they only make sense if the element is found) and raise an exception if the element is not found?