refactor using self-invoking functions