Refactored $timeout part not to invoke $apply.
Again some structure changes.