Reorganize code after changing database, part 1