added Alex's SQL connection leak fix to refactored branch