Fix #8 by switching to bit shifts and refactor redundant code.