Restructured code, added new helpers