partial foreign export implementation, some fixes