Refactor and make it parallel