Properly wait for process to finish