Refactor and make pipe friendly