Fixed various SQL related issues