added documentation and cleaned the code a bit