Working version: no known bugs or mem-leaks