Removed shared_ptr from contract method, replaced with class optional_ptr

Kind of a micro-optimization, but class shared_ptr seems to heavy-weight to be used inside the contract method unless necessary. I replaced it with a new class optional_ptr which combines functionality of raw pointer and unique_ptr depending on how it is initialized.