Refactor and add console logging