Added ending condition