Refactored and rewrote most of the db class, to be continued