F/fixes from 3.6

afsql: memleak fixed
control: useful refactor