code refactoring and minituraisation. Also made the methods more generic and protected/private. Started commenting.