currently the code inside cal controller that takes care of this task is too deeply nested and there is too much duplicate code