Refactor to reduce copying variables