Extracted some complexities into methods.