Updated code; testcases and document comments for opensourcing