logic refactor, debug option, 5XX error handling and removal of hard coded caste_id's