That's too much code and too vague a description I'm afraid. Please provide a more detailed description of what you're trying to achieve, exactly what you do when things go wrong and exactly what goes wrong.