Added Javadocs and split the code a bit