Reimplemented required logic from nested_set