Controlling competing photochemical reactions stabilizes perovskite solar cells