Simplify main() by moving content to functions