Several improvments and refactor