Optimize code and fix SQLite quotes (Issue #521)