Reworked db model and database functions