initial implementation for #379 but needs more testing