tidy up code (based on advice from scrutinizer)