added some comments for better readability and JavaDoc for better understanding.