invitition function improved