restructured code & cleanup