Added validation and a bit of cleanup