Added a bit more user logic to sql