Added error checks to encoding stage