Refactor code, fixed some invitations bugs