First draft of SP decode function.