Introduced inner iterator function to generate-pile function