Fixed bug with $this in an array being duplicated. Added a test to detect it in future.