refactored main() code