Some refactoring to fix some issues