Refactor and split code