more massive migration changes

encapsulating all SQL into SP's and migrating code