Added validation and other subtle improvements