awful code duplication, but avoids excessive allocation