Code cleanup and added commentaries in javadoc