Some atrocious code, but I'm certainly getting there...