Here i have added the snippet from my other thread, without making use of it. This is lengthy and hence may be hard to continue working on. Its length will reduce if I can integrate the parts after integrating the algorithm.