Modified according to code review by Loki Astari