refactor and fixes