code refactor. Stops getting table doesnt exist error