Improve type checking

It turns out the Closure Compiler does not type check return closures if the parameters are not explicitly specified.

This PR adds type checking for all returned closures, and fixes a couple of minor bugs/inconsistencies found by the compiler as a result. The improved type checking also helps the compiler generate fractionally smaller code.