use a flat stack instead of a recursive one