extracted code from main() & added some logging