fixed ending functionality