reduced the number of sql queries