Tidyup

Updated and reorganized some code