Clean up compiler warnings with some explicit casts and removing dead stores