Escape quotes in SQL

- Add IF EXISTS for globals (we need a better solution for this) 
- Minor changes to indexes