Changed to use simulated casting.
Added explanation of added code, so maintenance is easier.