Refactorings and SSH timeout fixes

This was originally #83, but I accidentally merged that and can't reopen it, so resubmitting this as a new PR.

This branch has been running on OS X production seemingly without issues for ~3 months.