Fix a bunch of type mistakes that relied on casting

Change-Id: If1dd6add7f0868d73b662e817843b8558a0f3866