spaghetti-code implementation of Hao Miao