refactor and fix