Changed db loading code into functions