Added documentation to code

inline documentation & minor implementation changes