tweaked logic to remove unnecessary code