The general rule is to follow your conscience. The conscience supersedes loyalty, which is why in the Ramayana, Vibhishan counselled Ravana and advised him not to follow the disastrous path. When Ravana did not pay heed, he switched sides to Rama because his conscience could not permit him to remain on the side of the unrighteous.