Fixed up code to a working state