sql refactoruing underway