Horribly complicated code!