improved some bit logic, added test print statements for debug