Security fix, added some more output validation