implement (hopefully) more robust logic