Separated some concerns, made it work fairly reliably.