Simplified code and moved non-UI code into threads