Fix 64-bit promotion and other bugs