refactored code and resolved specs issue