Refactored code for rake task