Completed main functions, refactoring repetition.