Performance improvements and SQL changes