Added some safety precautions and put returns where they needed to be.