Tidied up code a little, got rid of some warnings and added some comments