refactored ruby code