Improve implementation of hasEnded function