Refactor and support logging in