added missing 'var' declarations from _q_ values
lots of minor type improvements

In lots of small places in the code, the type definitions are a tad more
accurate.
Some places where variables held any, the type is now declared.