He thought that the "random occurrence" was just a random occurrence and the uchiha were wrongly blamed for it and thus prejudiced. That's what led up to the coup.According to who he thought was madara. Now that he knows the masked man is an uchiha and responsible for the 9 tails attack he is going to put his hate on the masked man. After all masked man is indirectly responsible for the prejudice towards his clan, and directly involved with the killing of his clan.
That is an assumption u made and he turned after hearing HASHIRAMA's definition of a ninja...he turned good not cause of vengeance but just because it would be a waste to turn on the village