Shifting some code around and added more JavaDoc