rearranged some of the code.

Moved the check for required fields under the check for already entered data.
Constructed returns if errors occur.
Renamed x to item.