extracted most code into three methods