Added event handlers and threading functionality