DRY up the logic

Thanks to hq1 of #erlang/freenode