Code duplications removed. Methods extracted for increased readability.