Fixed, corrected and enhanced the code

Corrected some serious array syntax faults, added a few functions and
corrected some comments.