New code

More efficient code, also works without hacky workarounds.