reorganized code, updated comments