insert function working in place of flatten