Moved code from codeplex