Modified factory to create tables only when required