Improved code for counter and shifter