Move end session to function