Please, use Code tags(with proper code indentations) when posting code snippets. Otherwise your code is absolutely unreadable.
Use debugger (Alt+F5) to debug this function to see where and why goes wrong.