Extracted most code into three methods