Added Factions support, cleaned up some code.