refactor and fix up