minior code improvements
fixed memory leak: queries weren't removed after selects
improved loops