Minor fixes.

Single initialization query.
Moved if out of loop.
Swallow unknown error.

TODO: fix IsValid logic. This takes most of the time in stressed applications.