Fixed using the wrong array key [0]. And improved code.
Corrected a variable name.