• ...almost identical code...

    When I run it directly...

    What exactly do you mean by these two points?

    Post your "almost identical" code too.