Completed debug code and abstracted log code. Prevented closing STD filehandles