Some refactoring to provide a better structure