fixing loops and subprocesses