Added comments, some fixes, timer improvement.

Added a lot of comments because this is such a long and confusing
controller. Made some changes that should make it work better. Also
made an improvement to the timer function so that it counts down till
the end time or the end of duration whichever is shorter.