need some comments and intermediate varibles here. This is getting long.