refactored decode to be much simpler and easier on the eyes