Add a few casts to make GCC happy.

Also adds a few imports and removes unused code.