Forgive me for how awful this code is :(