fix #581 + some other cleanup