Some refactoring/improvements