refactored to remove a few casts