added loops and changed some structure