Clean up code and fix zval ref