worked on CRM-12463 restructured code and fixes