Made some improvements on the algorithm that is determining if button is clicked