cleanup, add a few safety checks