Database refactor