Added detailed docs and some optimizations