Updated and finished code. added documentation.