First attempt at fix for reported bug

http://paste2.org/ha73hs2x