Added documentation, some code tweaks