[updated] functionize all code