Made the system less complex and reduced the code size

Made a `db()` and `query()` function to make future expansion easier. Reduced the amount of code it took to write the system.
You sure this will work ok in the empty dataset case?