multiple samples contributed by mjbrender

I've folded down the commits for @mjbrender and done some simple code clean up work.