rework to isolate SQL in seperate file