TRanslated some of the logic of staircases to pseudocode to make it easier to translate