Added lots of javadoc, fixed some functions