minor refactoring & added some precautions