improve sqls and the code in general