cleaned up the logic a bit