Try generalize algorithm.

Further generalization needs reimplementation of SmsState with saving of all elements in it.