Neatened up the code a lot