Refactor of database handles