I'd recommend to move that into a helper function... we're at quite some nesting level here ;)