Updated, tested and merged pzinovkin's patch for #56.

Tests pass on SQLite.