Modified previous solution to use iterative string insertion.