Added some typecasts. Still tracking down a memory problem...