Fixed major bug found by viewer

It's not in great shape as a script, but it's not returning faulty data
anymore.