Refactor build_tablename()