slight polishing, constans names changed, improved output