changed creation order and fixed a loop