added logic to create $ctx as a buffer