Added input argument checking and some code reorganization for clear flow
Added input argument checking and some code reorganization for clear flow

Trophime/singularity,Trophime/singularity,Trophime/singularity,Trophime/singularity