Restructure code. Refactor a couple of methods