improvements; refactoring

checking for invalid value significantly improved