Restructure code to make use of glib signals and split up code