Fixed variable overwrite issue which could cause sql errors