Validation and a bit of code cleanup