fixes and refactor