Fixed issue with cmd not waiting to complete