code cleanup and move sql from base to proc file