Cleans up some code that was repeated in two places.

Fixes 2987
http://redmine.virtualworldframework.com/issues/2987