Added documentation and did some refractoring