Refactor the whole thing