Optimize local marker loop

Replace unconditional 'for loop' with conditional 'while loop', script exits when it's job is done now.