Fix variables issue (set to global) and refine structure