fixes bug that leads to segfault

Added loop to wait for the UI to finish.