Error checking codes. Minor modifications,

This commit adds input data error checking in all functions. Several
minor modifications were also performed throughout the code.