Refactor and add creating a new thread for gui