Refactor into methods

Change-Id: Ifff9af6ebcbc54a01e694f921873900ed2498581