Refactor to separate some functionalities