Some refactoring to make the code more readable