Fixed reversed logic and added logging