small refactoring

1) put 3 times repeated loop in one place
2) delete redundant notnull check 
3) add check for whitespace value