added more documentation and reorganized code