Functional
Added several conditions for handling messy input data