+1. Since this logic is very complex, I will suggest to extract this into a Service-like object