cleaned up a bit, and added some comments
modified function exit statuses to match conventions