Refactor some code to avoid too long functions.