Code cleanup and optimization.  Known Working Implementation