Refactored the code to do some clean-up. Added more debug messages for testing. Changed unnecessary static variables to non-static.