Cleaned up code in patch from Kjetil Kjernsmo.