Refactor final solution