some refactoring and checking added