added headers and reorganized code