Add main() and refactoring

Add main() and refactoring