Reworked existing (still in progress) sqlite code