Extract db connection, some refactoring in model