Consider using a cast here in the else case to avoid checking for `isEmpty` twice.