mostly complete implementation