reduced code by merging code and adding bit counter lookup