More type fixes.

This patch also contains quite some cleanups and reorganization in runtime.js.