Reviewing and tweaking this code not looked at in 6 months