Refactor: extracted functions from main.