refactored; working well;