Fix most compiler warnings.
Add type annotations and missing declarations, remove dead code.