added logic to controller
fixed the insertions , handling of special cases