restructured and debugged