fix a bunch of implicit declarations
Merged upstream and other fixes