reorganized and restructured code