SQL injection hardening

A lot of the code was quite vulnerable.