Cleaning up some code and adding the separate thread.