Extracted duplicated code and added comments.