refactor and bugs