refactored and improved validation on inputs