Changed the loop structure

* Modified the loops so that i need to shutdown
* the exectuor at all
* Thanks to stackoverflow