begin refactor to make code more understandable