Begin to structure main properly

Also copy code from warden-linux to avoid inappropriate dependency.