moved out the loops to find spaces

to avoid confusion