Improved code and add Java doc