added some extra input checks.  code cleanup