SQL and RS closing + refactoring