Improved logic of roles assignment