Move db code to right place