SQL tweaks + code cleanup.