Class visibility and calling end() inside a function