SQL generation now basically working, albeit rather hackily