Improve singleton implementation

Still not thread-safe, but a bit better now. Should be made thread-safe
and maybe get converted to a template, if possible.