Tidied, and still works (well, it doesn't but I think that the errors are unrelated to this)