Impemented better photon count algorithm