refactoring to get rid of casts