Props to Anders J for the code.