restructured code, simplified