Few minor refactors
Factored out main logic
Remove unused code