First refactor, reorganize routes, and foreign keys