made more modular and better logging