query almost ready
optimized code
some bug fixes and improvements