Refactor startup scripts

Break up the logic into more testable functions