extract some code into separate methods to improve readability