Fix duplicated code for PR #152

*fix dupicated code and added it in a method