Refactor and clean up
Add logging