Small refactoring and added safety check.