refacted and added javadoc