Moved repeated code with db selection into environment.js