refactored: removed instanceof, added generics