Final version?

Tidied up code, catch more errors, give proper HTTP status codes
(right?).