use waitFor instead of looping