fix undefined index
some changes and fixes