Added some changes based on Ornicar's helpful code review. Remaining suggestions are listed in a gist.