improvements based on feedback from byrd (https://gist.github.com/webyrd/9217893)