Tidied everything up, currently works until self-reconnecting.