Improved validation and some cleanup