restructured code for brevity