Refactor to organize code better