changed some casting types to resolve memory leaks