Add tests, code documentation, refactoring.

any suggestions?