moving to a more functional style
returning an error from decode instead of throwing
tidying formatting