Updated code to near finished version