You’re on the right track. Try to simplify your logic first, along these lines:
Then build on it from there. [You can try example project here][2].