Reduce amount of sql queries