replaced old serialize code with working version