Add checks and exception handling

What it says on the tin.