Revised Nimit's code, had a few bugs, this is a fully working version including disambiguation