Improved Jappie's code a bit.