Reference counting

Implemented a very basic (and apparently slow) reference counting system