Rewrote SQLite DB for further optimization and bug fixes