Refactor to make code less ugly