Tidied up some messy looking code