Code is v messy, but got a thread sharing memory to work