Extract bit-stuff into separate functions so as to not repeat code.