Fix the return type and remove unneeded casts.
Add a number of more precise types and remove unneeded casts.