Refactor based on @peterbe's feedback