Fixed bugs. Added some documentation