overhauled event handling and background thread