somewhat more robust and cleaner