works on revision using Nimit's Second version of code