Refactoring to improve code and introduce validation