refactoring some things
again pushing more and more to sql