Added sender instanceof and cleaned up code