db queries optimization

Decreased db queries count from 62 to 2 (or 7 if include subqueries).
Still needs some optimization work.