Refactor to use more functions