added end logic