refactored init function