Code tidy up and final implementations.

Code tidy up and final implementations.