`__main__.py` has significant code duplication. It needs a refactoring to make it a bit better to maintain, test and avoid duplication.