Some cleaning, also moving debug function to sqlutil package.