code clean (based on original code from John Wharington)