fixed logically inverted code with the help of Brian Kirby. same functionality as before.