Derp. Found the real error. Remember how you were casting to uint8? 2^8..... =256. That's why all your numbers were 255. Cast to uint16 and it all works.