Refactor out vars and opening