Fixed several bugs with old SQL code.