Make code more modular, fix db connection bug