refactored to improve processing time