Code tidying and added documentation