Made the mysql script more robust