updated with Ady's code.