Reorder lifecycle events and fix indentation / linting warnings

Cherry picked #4 and fixed linting warnings.