re-arranged loops for inserting new object