Improvements to SQL and docs