reorganized code and added code docs