Finished revamping to handle nested SQL statements