Some more code cleanup. Getting rid of the pervasive pointer casting. Made the code more strongly typed. Locked implementation.