Refactoring and a bit of data validation