Bug fix: incorrect logic for checking and creating table