Changed scope of function and fix CS problem