Refactored and implemented most of the initialization logic.