working code

the code is working but need to add comments and repalce some of the
code.