refactor and small fixes