removing some dup'ed code and adding a little exception handling