Refactor and add functionality