Added some JavaDoc and did some optimization