Updated code with comments and test script