Refactor init function