Added Hanna's code, slightly corrected