Refactor code into database