Simplify and refactor alot of the database code, along with extracting some pieces of code into actual utilities.