Updated script so that it now works. Still weird loops though.