Add bit-twiddly stuff, reorganize things