Added some documentation plus  optimizations here and there