The piece of code I referred is http://stackoverflow.com/a/14857326/1162468